<html>
<head><meta charset="utf-8"><title>Wrapped type coercion through temporaries · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Wrapped.20type.20coercion.20through.20temporaries.html">Wrapped type coercion through temporaries</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202872671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Wrapped%20type%20coercion%20through%20temporaries/near/202872671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Wrapped.20type.20coercion.20through.20temporaries.html#202872671">(Jul 04 2020 at 12:55)</a>:</h4>
<p>Hi,<br>
Type coercion can fail when it is passed through a temporary, simplest case: <a href="https://play.rust-lang.org/?gist=487d56225ce4b06116eb357f75bf6474">https://play.rust-lang.org/?gist=487d56225ce4b06116eb357f75bf6474</a><br>
More interesting one: <a href="https://play.rust-lang.org/?gist=c5421fe405fbccf62782ec1718807e95">https://play.rust-lang.org/?gist=c5421fe405fbccf62782ec1718807e95</a></p>
<p>Is there anything I can do about it to make it work? <br>
my use-case is <a href="http://crates.io/crates/log-derive">crates.io/crates/log-derive</a> which is a proc-macro that wraps the content of a function in a closure so it can log the output before returning, but because of coercion this can break working code <br>
(the expanded output from my macro will be something like this: <a href="https://play.rust-lang.org/?gist=5d7b3f3addc12e0d08a6b7264619495a">https://play.rust-lang.org/?gist=5d7b3f3addc12e0d08a6b7264619495a</a>)</p>



<a name="202937125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Wrapped%20type%20coercion%20through%20temporaries/near/202937125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gary Guo <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Wrapped.20type.20coercion.20through.20temporaries.html#202937125">(Jul 05 2020 at 20:33)</a>:</h4>
<p>It fails because you cannot coerce <code>Option&lt;*mut T&gt;</code> into <code>Option&lt;*const _&gt;</code>. You can however coerce <code>*mut T</code> into <code>*const T</code></p>



<a name="202937190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Wrapped%20type%20coercion%20through%20temporaries/near/202937190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gary Guo <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Wrapped.20type.20coercion.20through.20temporaries.html#202937190">(Jul 05 2020 at 20:35)</a>:</h4>
<p>Coercion will not happen when there is type inference. In the <code>second</code> function, because you assign it to <code>res</code> which has its type inferred, there is no coercion, thus <code>res</code> got type <code>Option&lt;*mut T&gt;</code>, which cannot be coerced into the return type.</p>



<a name="202937244"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Wrapped%20type%20coercion%20through%20temporaries/near/202937244" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gary Guo <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Wrapped.20type.20coercion.20through.20temporaries.html#202937244">(Jul 05 2020 at 20:36)</a>:</h4>
<p>To avoid the error, give <code>res</code> an explicit type.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>